// map.js
import qqMap from "../../../utils/qqMap"
Page({
  data: {
    repairId: '',
    latitude: 40.060539,
    longitude: 116.343847,
    dialogVisible: false,
    statusMsg: {
      0: '已取消',
      1: '受理中',
      2: '上门中',
      3: '已完成',
    },
    statusType: {
      0: 'cancel',
      1: 'info',
      2: 'success',
      3: 'complete',
    },
    markers: [
      {
        id: 1,
        latitude: 40.086757,
        longitude: 116.328634,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 40.08346500000002,
        longitude: 116.33293800000011,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      },
    ],
  },
  onLoad({ id }) {
    this.getRepairDetail(id)
    this.setData({
      repairId: id
    })
    this.getPolyline()
  },
  //获取地图
  getPolyline() {
    qqMap.direction({
      mode: 'bicycling',//可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from: '40.060539,116.343847',
      to: '40.086757,116.328634',
      success: (res) => {
        console.log(res);
        let ret = res;
        let coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        let kr = 1000000;
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      }
    })
  },
  //获取查询报修详情信息
  async getRepairDetail(id) {
    const res = await wx.http.get(`/repair/${id}`)
    this.setData({
      ...res.data
    })
  },
  //点击取消报修显示弹出框
  showDialog() {
    this.setData({
      dialogVisible: true
    })
  },
  //点击确认取消按钮
  async dialogConfirm() {
    await wx.http.put(`/cancel/repaire/${this.data.repairId}`)
    wx.utils.toast('取消报修成功')
    wx.navigateBack()
  },
  //修改信息
  editMsg() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.repairId,
    })
  }
})
